/**
 *
 */
package demo.tellurium.module

import groovy.lang.MetaClass;


import demo.tellurium.framework.BaseUIModule;

/**
 * @author roger.xu
 *
 */
class CQPModule extends BaseUIModule {

    @Override
    public void defineUi() {
        ui.Container(uid: "CQPLogin", clocator: [tag: "form", method: "post", action: "j_security_check", name: "loginPage"]) {
            InputBox(uid: "Username", clocator: [tag: "input", type: "text", name: "j_username", id: "j_username"])
            InputBox(uid: "Password", clocator: [tag: "input", type: "password", name: "j_password", id: "j_password"])
            Button(uid: "SignIn", clocator: [tag: "button", type: "submit"])
        }

        ui.Container(uid: "CQPAllDocuments", clocator: [:]) {
            Container(uid: "SearchBox", clocator: [:]) {
                InputBox(uid: "SearchByDocumentName", clocator: [tag: "input", id: "documentName"])
                Container(uid: "SearchButton", clocator: [tag: "div", id: "searchBtn"]) {
                    UrlLink(uid: "Link", clocator: [tag: "a"])
                }
                Container(uid: "ClearResultButton", clocator: [tag: "div", id: "resetBtn"]) {
                    UrlLink(uid: "Link", clocator: [tag: "a"])
                }
                Container(uid: "AdvancedSearchButton", clocator: [id: "advancedSearchBtn"]) {
                    UrlLink(uid: "Link", clocator: [tag: "a"])
                }
            }
            Container(uid: "Toolbar", clocator: [id: "toolbar"]) {
                Span(uid: "CreateButton", clocator: [widgetid: "createBtn"])
                Span(uid: "OpenButton", clocator: [widgetid: "openButton"])
                Span(uid: "DeleteButton", clocator: [widgetid: "deleteButton"])
            }
            Container(uid: "TreeGrid", clocator: [id: "treegrid"]) {
                Container(uid: "Row", clocator: [tag: "tr", class: "dojoxGridRowToggle-0"]) {
                    CheckBox(uid: "CheckBox", clocator: [:])
                    Div(uid: "ExpandoNode", clocator: [class: "dojoxGridExpandoNode"], respond: ["click"])
                }
            }

        }
    }

    public String url = "http://9.186.10.174:8080/CQP4/user/alldocuments.jsp";

    public void doAdminLogin() {
        type "CQPLogin.Username", "saratoga@cn.ibm.com"
        pause 500
        type "CQPLogin.Password", "passw0rd"
        pause 500
        click "CQPLogin.SignIn"
        waitForPageToLoad 30000
    }

    public void doLogin(String username, String password) {
        type "CQPLogin.Username", username
        pause 500
        type "CQPLogin.Password", password
        pause 500
        click "CQPLogin.SignIn"
        waitForPageToLoad 30000
    }

    /* ---- SearchBox ---------------*/
    public void doSearch(String input) {
        type "CQPAllDocuments.SearchBox.SearchByDocumentName", input
        pause 500
        click "CQPAllDocuments.SearchBox.SearchButton.Link"
        waitForPageToLoad 30000
    }

    public void doClearResult() {
        click "CQPAllDocuments.SearchBox.ClearResultButton.Link"
        waitForPageToLoad 30000
    }

    public void doClickAdvancedSearch() {
        click "CQPAllDocuments.SearchBox.AdvancedSearchButton.Link"
        pause 500
    }

    /* ---- Toolbar ---------------*/
    public void doClickCreateButton() {
        def uid = "CQPAllDocuments.Toolbar.CreateButton";
        click uid
        pause 500
    }

    public void doClickOpenButton() {
        def uid = "CQPAllDocuments.Toolbar.OpenButton";
        click uid
        pause 500
    }

    public void doClickDeleteButton() {
        def uid = "CQPAllDocuments.Toolbar.DeleteButton";
        click uid
        pause 500
    }

    /* ---- Grid ---------------*/
    public void doClickCheckBox() {
        // dojo.query("#treegrid tr.dojoxGridRowToggle-1 input[type=checkbox]");
        def uid = "CQPAllDocuments.TreeGrid.Row.CheckBox";
        def nodeId = getAttribute(uid, "id");
        console.debug(">>>>>>>>", "ID:", nodeId);
        click uid
        pause 500
    }

    public void doExpandCell() {
        def uid = "CQPAllDocuments.TreeGrid.Row.ExpandoNode";
        click uid
        waitForPageToLoad 30000
    }

}
